#!/usr/bin/env python
# -*- coding:utf-8 -*-
import subprocess
from time import sleep
from operate.log import logger


def long_press_home():
    # 使用发送事件方式，实现长按Home键操作
    # 此方法无法实现长按Home键选择启动器操作
    press_home_down = 'adb shell sendevent /dev/input/event0 1 158 1 &&' \
                      'adb shell sendevent /dev/input/event0 0 0 0'

    press_home_up = 'adb shell sendevent /dev/input/event0 1 158 0 &&' \
                    'adb shell sendevent /dev/input/event0 0 0 0'

    with subprocess.Popen(press_home_down,
                          shell=True,
                          stdin=subprocess.PIPE,
                          stdout=subprocess.PIPE,
                          stderr=subprocess.PIPE
                          ) as down:
        logger.info("press home down")
        down_std = down.stderr.readlines()
        if down_std:
            logger.error("press home down stderr:" + str(down_std))
    sleep(0.5)
    with subprocess.Popen(press_home_up,
                          shell=True,
                          stdin=subprocess.PIPE,
                          stdout=subprocess.PIPE,
                          stderr=subprocess.PIPE,
                          ) as up:
        logger.info("press home up")
        up_std = up.stderr.readlines()
        if up_std:
            logger.error("press home up stderr:" + str(up_std))
    if down.returncode or up.returncode:
        logger.warning("Long Press Home Error")

    up.kill()
    down.kill()

if __name__ == '__main__':
    long_press_home()

